package com.ellecity06.common.base

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import androidx.viewbinding.ViewBinding
import com.ellecity06.common.R
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import androidx.core.graphics.drawable.toDrawable
import com.gyf.immersionbar.ImmersionBar
import com.gyf.immersionbar.ktx.immersionBar


/**
 * @author ellecity06
 * @time 2024/11/29 14:21
 * @des
 */
abstract class BaseBottomDialogFragment<VB : ViewBinding> : BottomSheetDialogFragment() {
    protected lateinit var binding: VB
    open val gravity: Int = Gravity.CENTER
    open val animation: Int = -1
    open val isTouchOutsideDismiss: Boolean = true
    open val dimAmount: Float = 0.5f
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = inflateBinding(inflater, container)
        val params = dialog!!.window?.attributes
        params?.gravity = gravity

        //        params.windowAnimations = R.style.bottomSheet_animation;//动画
        params!!.dimAmount = dimAmount
        dialog!!.window!!.attributes = params
        dialog!!.window!!.requestFeature(Window.FEATURE_NO_TITLE)
        dialog!!.setCanceledOnTouchOutside(isTouchOutsideDismiss)
        immersionBar {
            navigationBarColor(R.color.white)
            navigationBarEnable(false)
            navigationBarWithKitkatEnable(false)
        }
//        dialog!!.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        return binding.root
    }

    //    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
//        val dialog = super.onCreateDialog(savedInstanceState)
//
//        // 设置背景透明度
//        if (dialog.window != null) {
//            dialog.window!!.setDimAmount(dimAmount)
//            dialog.window!!.setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
//        }
//        return dialog
//    }
    private fun initAttr() {
        val params = dialog!!.window?.attributes
        params?.gravity = gravity

        //        params.windowAnimations = R.style.bottomSheet_animation;//动画
        params!!.dimAmount = dimAmount
        dialog!!.window!!.attributes = params
        dialog!!.window!!.requestFeature(Window.FEATURE_NO_TITLE)
        dialog!!.setCanceledOnTouchOutside(isTouchOutsideDismiss)

        dialog!!.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initialize()

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.BottomSheetDialogNormalStyle)
    }

    abstract fun inflateBinding(inflater: LayoutInflater, container: ViewGroup?): VB
    abstract fun initialize()
}